/*
 * disco.c
 *
 * Created: 21.02.2013 19:56:41
 *  Author: PimpFoReal
 */ 


#include <avr/io.h>
#include <avr/interrupt.h>

#define LED0 0
#define LED1 3
#define LED2 1
#define LED3 2

#define LED_PORT PORTB
#define LED_DDR  DDRB
#define ON 1
#define OFF 0



int main(void){

	
	//Set the leds as outputs.  0 = input, 1 = output
	LED_DDR = (1 << LED0) | (1 << LED1) | (1 << LED2) | (1 << LED3);
	//All led initially off
	LED_PORT = 0b00001111;
	//Clock select
	TCCR1B |= (1 << WGM12); // Timer in CTC mode (invokes interrupt when count finish)
	TCCR1B |= (1 << CS12) | (1 << CS10); //Prescale clk/1024 and start clock
	OCR1A = 10000;
	TIMSK1 |= (1 << OCIE1A); // Enable interupt

	sei();
    while(1)
    {
        //TODO:: Please write your application code 
    }
}


char read_led(uint8_t led) {
	if (LED_PORT & (1 << led)) {
		return OFF; //1 betyr av, 0 betyr paa
	} else {
		return ON;
	}
}

void led_on (uint8_t led){
	// write the LED_PORT register to set the led
	// How can you do this without affecting the other leds?
	// remeber that the led is on when the output is 0/GND!
	LED_PORT &= ~(1 << led); // 0 = ON

}
void led_off(uint8_t led){
	// write the LED_PORT register to clear the led
	LED_PORT |= (1<< led); // 1 = OFF
}

ISR(TIMER1_COMPA_vect) {
	if (!read_led(LED0)) {
		led_on(LED0);
	} else if(!read_led(LED1)) {
		led_on(LED1);
		led_off(LED0);
	} else if (!read_led(LED2)) {
		led_off(LED0);
		led_off(LED1);
		led_on(LED2);
	} else if(!read_led(LED3)) {
		led_off(LED0);
		led_off(LED1);
		led_off(LED2);
		led_on(LED3);	
	} else {
		led_off(LED0);
		led_off(LED1);
		led_off(LED2);
		led_off(LED3);
	}		
}